package com.qianphone.mylbs;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;

public class MyLocationActivity extends FragmentActivity {

	private BaiduMap mBaiduMap;
	private MapView mMapView;

	// 实现定位的类
	private LocationClient mLocClient;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		SDKInitializer.initialize(getApplicationContext());

		setContentView(R.layout.my_location_activity);

		// 地图初始化

//		SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
//				.findFragmentById(R.id.map_fragment);

		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();

		mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(LocationMode.NORMAL, true,
				null));
		// 开启定位图层
		mBaiduMap.setMyLocationEnabled(true);
		// 定位初始化
		mLocClient = new LocationClient(this);
		// 给定位对象设置以坐标更新的监听器
		mLocClient.registerLocationListener(new MyLocationListener());

		// 创建一个定位的设置参数
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		// 坐标更新的间隔时间
		option.setScanSpan(1000);
		// 把定位参数设置给定位对象
		mLocClient.setLocOption(option);
		// 开始定位
		mLocClient.start();
	}

	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// 退出时销毁定位
		mLocClient.stop();
		// 关闭定位图层
		mBaiduMap.setMyLocationEnabled(false);
		mMapView.onDestroy();
		mMapView = null;
		super.onDestroy();
	}

	private class MyLocationListener implements BDLocationListener {

		/**
		 * 当接收到一个新的坐标位置的时候会被调用
		 */
		@Override
		public void onReceiveLocation(BDLocation loc) {
			// 获取纬度
			double latitude = loc.getLatitude();
			// 获取经度
			double longitude = loc.getLongitude();

			String address = loc.getAddrStr();

			System.out.println("latitude = " + latitude + " longitude = " + longitude
					+ " address = " + address);

			LatLng latLng = new LatLng(latitude, longitude);
			MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(latLng);
			mBaiduMap.animateMapStatus(u);

			MyLocationData locData = new MyLocationData.Builder().accuracy(loc.getRadius())
			// 此处设置开发者获取到的方向信息，顺时针0-360
					.direction(loc.getDirection()).latitude(latitude).longitude(longitude).build();
			mBaiduMap.setMyLocationData(locData);
		}

	}
}